package io.github.wslxm.springbootplus2.manage.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.wslxm.springbootplus2.constant.CacheKey;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.base.service.impl.BaseServiceImpl;
import io.github.wslxm.springbootplus2.core.enums.BaseDic;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.core.utils.bean.XjSpringContextUtil;
import io.github.wslxm.springbootplus2.manage.sys.mapper.SysBlacklistMapper;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysBlacklistDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysBlacklist;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysBlacklistQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysBlacklistVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysBlacklistService;
import io.github.wslxm.springbootplus2.starter.redis.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 黑名单
 * <p>
 * ::本代码由[兮家小二]提供的代码生成器生成,如有问题,请手动修改 ::作者CSDN:https://blog.csdn.net/qq_41463655
 * </p>
 *
 * @author wangsong
 * @email 1720696548@qq.com
 * @date 2020-11-27 22:44:49
 */
@Service
public class SysBlacklistServiceImpl extends BaseServiceImpl<SysBlacklistMapper, SysBlacklist> implements SysBlacklistService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public BasePage<SysBlacklistVO> findPage(SysBlacklistQuery query) {
        LambdaQueryWrapper<SysBlacklist> queryWrapper = new LambdaQueryWrapper<SysBlacklist>()
                .orderByDesc(SysBlacklist::getCreateTime)
                .eq(query.getType() != null, SysBlacklist::getType, query.getType())
                .eq(query.getDisable() != null, SysBlacklist::getDisable, query.getDisable())
                .eq(StringUtils.isNotBlank(query.getIp()), SysBlacklist::getIp, query.getIp());
        return XjBeanUtil.pageVo(this.page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper), SysBlacklistVO.class);
    }


    @Override
    public List<SysBlacklistVO> listByType(Integer type) {
        List<SysBlacklist> list = this.list(new LambdaQueryWrapper<SysBlacklist>()
                .orderByDesc(SysBlacklist::getCreateTime)
                .eq(SysBlacklist::getType, type)
                .eq(SysBlacklist::getDisable, BaseDic.Whether.V0.getValue())
        );
        return XjBeanUtil.listVo(list, SysBlacklistVO.class);
    }


    @Override
    public List<String> findBlacklistByType(Integer type) {
        // 调用方法处理数据
        List<SysBlacklistVO> blacklistVos = listByType(type);
        if (!blacklistVos.isEmpty()) {
            return blacklistVos.stream().map(SysBlacklistVO::getIp).collect(Collectors.toList());
        }
        return new ArrayList<>(0);
    }


    @Override
    @Cacheable(cacheResolver = CacheKey.XJ_RESOLVER, value = CacheKey.BLACK_LIST_BY_TYPE, key = "#type")
    public List<String> findBlacklistByType(Integer type) {
        // 调用方法处理数据
        List<SysBlacklistVO> blacklistVos = this.listByType(type);
        if (!blacklistVos.isEmpty()) {
            return blacklistVos.stream().map(SysBlacklistVO::getIp).collect(Collectors.toList());
        }
        return new ArrayList<>(0);
    }

    @Override
    public String insert(SysBlacklistDTO dto) {
        SysBlacklist entity = dto.convert(SysBlacklist.class);
        boolean b = this.save(entity);
        this.delCache();
        return entity.getId();
    }

    @Override
    public Boolean upd(String id, SysBlacklistDTO dto) {
        SysBlacklist entity = dto.convert(SysBlacklist.class);
        entity.setId(id);
        boolean b = this.updateById(entity);
        this.delCache();
        return b;
    }

    @Override
    public Boolean del(String id) {
        boolean b = this.removeById(id);
        this.delCache();
        return b;
    }


    /**
     * 缓存清除
     * <p>
     * 在多租户的情况下，让其能清除所有租户的缓存
     * <p/>
     */
    private void delCache() {
        Set<String> keys = redisUtil.getKeysByLike(CacheKey.BLACK_LIST_BY_TYPE);
        redisUtil.delete(keys);
    }
}
